package teehee.Form;

import java.util.ArrayList;

import teehee.Action.AddNewAction;
import teehee.DB.DB;

import android.content.Context;

/**
 * Validates data for Create New Exercise/Create New WOD Page.
 * 
 * For Create New Exercise, we need to validate:
 *  - Name: Check if it is null or if it contains special characters and white space
 * 
 * For Create New WOD, we need to validate:
 *  - Name: Check if it is null or if it contains special characters or consecutive space.
 *  - Exercises: Verify that at least one exercise is added.
 *  - Method of Measurement: Verify that either "timed" or "rep" is selected.
 *  - Weight: Only number is allowed.
 * 
 * @author 
 *
 */
public class AddNewForm 
{
	/**
	 * Check if the name is null or if it contains special characters or consecutive space.
	 * @param  name	name of the WOD or exercise.
	 * @return		integer that indicate the validation condition,
	 * 				-1 if name is null, 0 if it contains special characters or consecutive space,
	 * 				1 if it is valid.
	 */
	public static int validateName(String name)
	{
		//check if the name is null or empty
		if (name == null || name.equals("")) {
			return -1;
		} else {
			boolean [] spaceFlag = new boolean[name.length()];
			name = name.trim();
			for (int i = 0; i < name.length() ; i++) {
				char c = name.charAt(i);
				if (c == 32) {
					spaceFlag[i] = true; //track the positions of space
				}
				if (c != 32 && c < 48 || (c > 57 && c < 65) || c > 122) { //check special char
					return 0;
				}
			}
			// if spaces are consecutive, return 0
			for(int i = 1; i < name.length(); i++)
			{
				if(spaceFlag[i] == true && spaceFlag[i-1] == true)
				{
					return 0;
				}
			}
			
			return 1;
		}
	}
	
	/**
	 * Validate the format of reps/time. 
	 * 
	 * @param	type	Type of the measurement.
	 * 			input	Input of the measurement.
	 * @return			true if the format is correct.
	 */
	public static boolean validateMeasurement(String type, String input)
	{
		if(input == null || input.equals("") )
		{
			return false;
		}
		input = input.trim();
		if(type.equals("Timed"))
		{
			if(input.length() == 0)
			{
				return false;
			}
			input = input.trim();
			if(!(input.matches (":\\d\\d") || input.matches("(\\d\\d:){1,2}?(\\d\\d)"))) {
				return false;
			}

			for(String s : input.split(":")) {
				if((s = s.trim()).length() > 0) {
					if(!(s.matches("\\d+?") && Integer.parseInt(s) < 60)) {
						return false;
					}
				}
			}
			return true;
		}
		else
		{
			boolean [] dashFlag = new boolean[input.length()];
			//if the user did not input anything for the reps
			if(input.length() == 0) //Could have just done like 5 or 10 reps.
			{
				return false;
			}
			//if the first character is not a number
			if(input.charAt(0) < 48 || input.charAt(0) > 57)
			{
				return false;
			}
			
			//if the last character is not a number
			if(input.charAt(input.length()-1) < 48 || input.charAt(input.length()-1) > 57)
			{
				return false;
			}
			
			for (int i = 1; i < input.length()-1 ; i++) {
				if ((input.charAt(i) < 48 || input.charAt(i) > 57) && input.charAt(i) != 45) {
					return false;
				}
			}
			for(int i = 0; i < input.length(); i++) // something like 23- or like 23-----23
			{
				char c = input.charAt(i);
				if(c == 45)
				{
					dashFlag[i] = true;
				}
			}
			for(int i = 1; i < input.length(); i++)
			{
				char c = input.charAt(i);
				if(c == 45 && dashFlag[i-1] == true)
				{
					return false;
				}
			}
		}
		return true;
	}
	
	/**
	 * Description goes here... 
	 * 
	 * @throws
	 * @return
	 * @param
	 */
	public static boolean determineMeasurement(String type)
	{
		if(type.equals("Reps"))
		{
			return true;
		}
		return false;
	}
	
	public static boolean validateWeight(String weight)
	{
		if (weight == null || weight.equals("")) {
			return true;
		}
		weight = weight.trim();
		for (int i = 0; i < weight.length() ; i++) {
			if (weight.charAt(i) > 57 || weight.charAt(i) < 48) {
				return false;
			}
		}
		return true;
	}
	
	public static DB AddNewFormFind(String name)
	{
		DB entry = AddNewAction.AddNewActionFind(name);
		return entry;
	}
	
	public static boolean createNewEntryForm(Context ctx, String type, String name,
			String reps, String time, String weights, String comments, ArrayList<String> exercises) {
		return AddNewAction.createNewEntryAction(ctx, type, name, reps, time, weights, comments, exercises);
	}
}
